using System;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Resources;
using Microsoft.Office.Interop.Word;
using System.Windows.Forms;
using Send2Sharepoint.Common;

namespace Send2Sharepoint.Word
{
    class UploadWordDocument:WorkItem
    {
        private readonly _Document _wordDocument;
        readonly ResourceManager _rm = new ResourceManager("Send2Sharepoint.Resources", Assembly.GetExecutingAssembly());

        public _Document WordDocument
        {
            get { return _wordDocument; }
        }

        public UploadWordDocument(_Document document)
        {
            _wordDocument = document;
        }

        internal override void Upload(BackgroundWorker worker, DoWorkEventArgs e)
        {
            if (WordDocument != null)
            {
                var originalFileName = WordDocument.FullName;
                var fileName = WordDocument.Name;
                var originalApp = WordDocument.Application;
                Logging.WriteToLog(WordDocument.FullName);
                try
                {
                    CloseSingleDocument(WordDocument.Application, WordDocument.Name);
                    var fileData = Globals.GetFileData(originalFileName);
                    var repository = new Repository.Repository(Globals.UserName, Globals.Password, Globals.SharepointSiteUrl);
                    var destinationUrl = Globals.GetDestinationUrl(fileName);
                    repository.UploadFile2007(destinationUrl, fileData, IndexInformation);
                    OpenSingleDocument(originalApp, originalFileName);
                }
                catch (Exception ex)
                {
                    OpenSingleDocument(originalApp, originalFileName);
                    Logging.WriteException(ex);
                    throw;
                }
            }
            else
            {
                MessageBox.Show(string.Format(_rm.GetString("NotSavedMessage"), "Workbook"), _rm.GetString("ApplicationName"), MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }
        
        internal static void CloseSingleDocument(_Application appId, string documentName)
        {
            object oDocIndex = documentName;
            try
            {
                var doc = appId.Documents.get_Item(ref oDocIndex);
                if (doc != null)
                {
                    object saveChanges = false;
                    object originalFormat = null;
                    object routeDoc = null;
                    doc.Close(ref saveChanges, ref originalFormat, ref routeDoc);
                }
            }
            catch(Exception ex)
            {
                Logging.WriteException(ex);
            }
        }
        internal static void OpenSingleDocument(_Application wordApp, object fileName)
        {
            object missing = Missing.Value;

            try
            {
                _Document aDoc = wordApp.Documents.Open(ref fileName, ref missing, ref missing,
                                                                           ref missing, ref missing, ref missing, ref missing, ref missing,
                                                                           ref missing, ref missing, ref missing, ref missing,
                                                                           ref missing, ref missing, ref missing, ref missing);
                aDoc.Activate();
            }
            catch (Exception ex)
            {
                Logging.WriteException(ex);
            }
        }
    }
}
